package com.pf.rule.service.impl;

import cn.hutool.core.util.IdUtil;
import com.google.common.collect.Lists;
import com.pf.common.utils.DTOUtils;
import com.pf.log.processor.SimpleLogProcessor;
import com.pf.rule.dto.RuleAddReq;
import com.pf.rule.dto.RuleUpdateReq;
import com.pf.rule.mapper.RuleMapper;
import com.pf.rule.mapper.entity.RuleEntity;
import com.pf.rule.service.RuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author PanFei
 * @version 1.0.0
 * @createTime 2022/7/7 23:34
 */
@Service
public class RuleServiceImpl implements RuleService {

    @Autowired
    private RuleMapper ruleMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addRule(RuleAddReq ruleReq) {
        // 1.新增规则数据信息
        RuleEntity ruleEntity = DTOUtils.copyToNewObj(ruleReq, RuleEntity.class);
        ruleEntity.setCreatedBy("system");
        ruleEntity.setCreatedTime(new Date());
        ruleEntity.setIsDeleted(0);
        ruleEntity.setUpdatedBy("system");
        ruleEntity.setUpdatedTime(new Date());
        String ruleCode = IdUtil.fastSimpleUUID();
        ruleEntity.setRuleCode(ruleCode);
        ruleMapper.insert(ruleEntity);
        // 2.新增操作日志
        Map<String, Map<String, String>> dataDictMap = getDataDictMap();
        SimpleLogProcessor simpleLogProcessor = new SimpleLogProcessor(dataDictMap);
        simpleLogProcessor.asyncRecordAddLog(Lists.newArrayList(ruleEntity));
    }

    @Override
    public void updateRule(RuleUpdateReq ruleReq) {
        RuleEntity oldRule = ruleMapper.selectById(ruleReq.getId());
        if (oldRule == null) {
            throw new RuntimeException("规则信息不存在！id=" + ruleReq.getId());
        }
        RuleEntity updatedRule = DTOUtils.copyToNewObj(oldRule, RuleEntity.class);
        DTOUtils.copyToObj(ruleReq, updatedRule);
        ruleMapper.updateById(updatedRule);
        // 记录修改操作日志
        Map<String, Map<String, String>> dataDictMap = getDataDictMap();
        SimpleLogProcessor simpleLogProcessor = new SimpleLogProcessor(dataDictMap);
        simpleLogProcessor.asyncRecordModifyLog(Lists.newArrayList(oldRule), Lists.newArrayList(updatedRule));
    }

    @Override
    public void deleteRule(Integer id) {
        RuleEntity oldRule = ruleMapper.selectById(id);
        if (oldRule == null) {
            throw new RuntimeException("规则信息不存在！id=" + id);
        }
        ruleMapper.updateIsDelete(id, 1, "system");
        // 记录删除操作日志
        SimpleLogProcessor simpleLogProcessor = new SimpleLogProcessor();
        simpleLogProcessor.recordDeleteLog(Lists.newArrayList(oldRule));
    }

    @Override
    public void recoverRule(Integer id) {
        RuleEntity oldRule = ruleMapper.selectById(id);
        if (oldRule == null) {
            throw new RuntimeException("规则信息不存在！id=" + id);
        }
        ruleMapper.updateIsDelete(id, 0, "system");
        // 记录删除操作日志
        SimpleLogProcessor simpleLogProcessor = new SimpleLogProcessor();
        simpleLogProcessor.recordRecoverLog(Lists.newArrayList(oldRule));
    }


    /**
     * 获取数据字典信息
     *
     * @return
     */
    private static Map<String, Map<String, String>> getDataDictMap() {
        Map<String, Map<String, String>> dataDictMap = new HashMap<>();
        // 制度类型数据字典
        Map<String, String> ruleTypeMap = new HashMap<>();
        ruleTypeMap.put("1", "工作制度");
        ruleTypeMap.put("2", "生活制度");
        dataDictMap.put("ruleType", ruleTypeMap);
        return dataDictMap;
    }
}
